package cn.tedu.hotel.service.impl;

import cn.tedu.hotel.ex.ServiceException;
import cn.tedu.hotel.mapper.AlbumMapper;
import cn.tedu.hotel.mapper.PictureMapper;
import cn.tedu.hotel.pojo.dto.PictureAddNewDTO;
import cn.tedu.hotel.pojo.entity.Picture;
import cn.tedu.hotel.pojo.vo.PictureListItemVO;
import cn.tedu.hotel.pojo.vo.PictureStandardVO;
import cn.tedu.hotel.service.IPictureService;
import cn.tedu.hotel.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 *  处理图片数据业务的实现类
 */
@Slf4j
@Service
public class PictureServiceImpl implements IPictureService, ServiceCode {

    @Autowired
    private PictureMapper pictureMapper;


    public PictureServiceImpl() {
        log.info("创建业务对象：PictureServiceImpl");
    }

    @Override
    public void addNew(PictureAddNewDTO pictureAddNewDTO) {
        log.debug("开始处理【添加图片】的业务，参数：{}", pictureAddNewDTO);
        // 获取当前时间：LocalDateTime now = LocalDateTime.now()
        LocalDateTime now = LocalDateTime.now();
        // 创建对象
        Picture picture = new Picture();
        // 复制属性
        BeanUtils.copyProperties(pictureAddNewDTO, picture);
        // 补全各属性的值：gmtCreate / gmtModified
        picture.setGmtCreate(now);
        picture.setGmtModified(now);
        // 执行插入数据
        int rows = pictureMapper.insert(picture);
        if (rows != 1) {
            String message = "添加图片失败！服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ERR_INSERT, message);

        }
    }

        @Override
        public void deleteById (Long id){
            log.debug("开始处理【删除图片】的业务，参数：{}", id);
            // 检查尝试删除的图片是否存在
            Object queryResult = pictureMapper.getStandardById(id);
            if (queryResult == null) {
                throw new ServiceException(ERR_NOT_FOUND, "删除图片失败，尝试访问的数据不存在！");
            }


            // 执行删除
            log.debug("即使删除id为{}的图片……", id);
            int rows = pictureMapper.deleteById(id);
            if (rows != 1) {
                throw new ServiceException(ERR_DELETE, "删除图片失败，服务器忙，请稍后再次尝试！");
            }
            log.debug("删除完成！");
    }

    @Override
    public void setIsCover(Long id) {
        log.debug("开始处理【设置为封面】的业务，id={}",id);
        //根据id获取图片的数据
        PictureStandardVO queryResult = pictureMapper.getStandardById(id);
        //判断结果是否为null
        if (queryResult == null){
            String message = "设置封面失败，访问数据不存在";
            log.warn(message);
            throw new ServiceException(ERR_NOT_FOUND,message);
        }

        //判断is_cover 是否为1
        if (queryResult.getIsCover() == 1){
            String message = "设置封面失败，此图片已经是封面";
            log.warn(message);
            throw new ServiceException(ERR_CONFLICT,message);
        }

        //创建对象，并设置为封面
        Picture picture = new Picture();
        picture.setId(id);
        picture.setIsCover(1);

        //执行更新，获取返回值
        int rows = pictureMapper.update(picture);
        if (rows != 1){
            String message = "设置封面失败，服务器繁忙，请稍后再试";
            log.warn(message);
            throw new ServiceException(ERR_UPDATE,message);
        }
    }

    @Override
    public void setNotCover(Long id) {
        log.debug("开始处理【设置未启用为封面】的业务，id={}",id);
        //根据id获取图片的数据
        PictureStandardVO queryResult = pictureMapper.getStandardById(id);
        //判断结果是否为null
        if (queryResult == null){
            String message = "设置未启用为封面，访问数据不存在";
            log.warn(message);
            throw new ServiceException(ERR_NOT_FOUND,message);
        }

        //判断is_cover 是否为0
        if (queryResult.getIsCover() == 0){
            String message = "设置未启用为封面，此图片未启用为封面";
            log.warn(message);
            throw new ServiceException(ERR_CONFLICT,message);
        }

        //创建对象，并设置为不是封面
        Picture picture = new Picture();
        picture.setId(id);
        picture.setIsCover(0);

        //执行更新，获取返回值
        int rows = pictureMapper.update(picture);
        if (rows != 1){
            String message = "设置未启用为封面，服务器繁忙，请稍后再试";
            log.warn(message);
            throw new ServiceException(ERR_UPDATE,message);
        }
    }


    @Override
    public List<PictureListItemVO> listPicture() {
        return pictureMapper.listPicture();
    }

}
